﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Net;
//using System.ServiceModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Kaskelot.SP.UI.Analys.Domain;
using Telerik.Windows.Controls;
using Kaskelot.SP.UI.Analys.Data;
using Kaskelot.SP.UI.Analys.ViewModel;
using System.Globalization;
using System.Resources;
using System.Reflection;
using Telerik.WebUI;

namespace Kaskelot.SP.UI.Analys.Controls
{

    public partial class GridContainer : UserControl
    {
		public class CustomLocalizationManager : LocalizationManager
		{
			public override string GetStringOverride(string key)
			{
				switch (key)
				{
					case "GridViewGroupPanelText":
						return "Dra kolumner hit för att gruppera dem";
				}
				return base.GetStringOverride(key);
			}
		}

        public GridContainer()
        {
            InitializeComponent();
            Loaded += new RoutedEventHandler(GridContainer_Loaded);

			LocalizationManager.Manager = new CustomLocalizationManager();
			Grid.Rebind();
        }

        void GridContainer_Loaded(object sender, RoutedEventArgs e)
        {

        }

        internal void SetFields(List<Field> list)
        {
            Grid.Columns.Clear();
            Grid.Columns.AddRange(list.Select(f => CreateColumn(f.DataTypeInGrid, f.InternalName, f.NameInGrid)));
        }

        public void SetHeight(int value)
        {
            Grid.Height = (double)value;
        }

        public void SetData(object data)
        {
            Grid.ItemsSource = data;
        }

        private static GridViewColumn CreateColumn(Type columnType, string dataPath, string header)
        {
            GridViewDataColumn col = new GridViewDataColumn();
            col.DataType = columnType;
            col.UniqueName = dataPath;
            col.Header = header;
            col.Width = 200;
            col.TextWrapping = TextWrapping.Wrap;
            col.DataMemberBinding = new Binding("[" + dataPath + "]");

            if (columnType == typeof(DateTime))
            {
                col.DataMemberBinding.StringFormat = "dd/MM/yyyy";
                col.DataMemberBinding.ConverterCulture = new System.Globalization.CultureInfo("en-GB");
            }
            return col;
        }

        #region Export to excel

        private void ExportButton_Click(object sender, RoutedEventArgs e)
        {
            string extension = "xml";
            ExportFormat format = ExportFormat.ExcelML;


            SaveFileDialog dialog = new SaveFileDialog();
            dialog.DefaultExt = extension;
            dialog.Filter = String.Format("Excel xml files (*.{0})|*.{0}|All files (*.*)|*.*", extension);
            dialog.FilterIndex = 1;

            if (dialog.ShowDialog() == true)
            {
                using (Stream stream = dialog.OpenFile())
                {
                    GridViewExportOptions exportOptions = new GridViewExportOptions();
                    exportOptions.Format = format;
                    exportOptions.ShowColumnFooters = true;
                    exportOptions.ShowColumnHeaders = true;
                    exportOptions.ShowGroupFooters = false;

                    Grid.Export(stream, exportOptions);
                }
            }
        }

        #endregion

    }
}
